package com.mrd.gtimp.business.service;

import com.mrd.base.BaseBiz;
import com.mrd.gtimp.business.entity.ContacterModel;
import com.mrd.gtimp.business.entity.GoodAddressModel;
import com.mrd.gtimp.business.entity.TrailModel;
import com.mrd.spring.repository.BaseRepository;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

@Service
public class TrailModelBiz extends BaseBiz<TrailModel> {

    @Resource
    private ContacterModelBiz contacterModelBiz;


    @Resource
    // 该处指定注入BaseRepository的dao对象
    @Qualifier("trailModelDao")
    public void setBaseRepository(BaseRepository<TrailModel, String> baseRepository) {
        super.baseRepository = baseRepository;
    }
    /*
     * (non-Javadoc)
	 * @see
	 * com.mrd.gtimp.business.biz.BaseBusinessModelBiz#saveBusiness(com.mrd.gtimp.business.pojo.BaseBusinessModel
	 * )
	 */

    public void saveBusiness(TrailModel trail, List<String> properties) throws Exception {
        if (null != trail) {

            // 建立临时list存放保存过的联系人和货物列表联系人
            Set<ContacterModel> tempList = new HashSet<ContacterModel>();
            // 联系人
            contacterModelBiz.saveConcater(trail.getShipper(), trail.getCustomer(), trail.getCreator());
            tempList.add(trail.getShipper());// 20140311
            List<ContacterModel> conList = new ArrayList<ContacterModel>();
            for (GoodAddressModel goodAddress : trail.getGdes()) {
                // 检测是否有相同的联系人和货物联系人，如果有就不执行saveConcater
                conList.add(goodAddress.getContacter());
                conList.removeAll(tempList);
                for (ContacterModel contacter : conList) {
                    if (trail.getShipper() == null
                            || goodAddress.getContacter().getId() == null || !goodAddress.getContacter().getId().equals(contacter.getId())
                            || !trail.getShipper().getId().equals(contacter.getId())) {
                        // 保存地址联系人
                        contacterModelBiz
                                .saveConcater(goodAddress.getContacter(), trail.getCustomer(), trail
                                        .getCreator());
                        tempList.add(goodAddress.getContacter());
                    }
                }

                // 保存地址
                // goodAddressBiz.saveOrupdateEntity(goodAddress);
                goodAddress.setTrail(trail);
                if ("".equals(goodAddress.getId())) {
                    goodAddress.setId(null);
                }
            }

            // 保存拖车信息
            if (StringUtils.isEmpty(trail.getId())) {
                trail.setId(null);
                this.saveOrupdateEntity(trail);
            } else {
                // 删除原有的拖车地址重新加入
                StringBuffer sql = new StringBuffer();
                sql.append("delete from com.mrd.gtimp.business.pojo.GoodAddressModel g where g.trail.id = '")
                        .append(trail.getId()).append("'");
                if (trail.getGdes() != null && trail.getGdes().size() != 0) {
                    sql.append("and g.id not in (");
                    for (GoodAddressModel gd : trail.getGdes()) {
                        sql.append("'").append(gd.getId()).append("',");
                    }
                    sql.deleteCharAt(sql.length() - 1);
                    sql.append(")");
                }
                this.baseRepository.delete(sql.toString(), null);
                this.updateEntity(trail, properties);
            }
        }
    }
}
